//
//  FLMManagerOrderController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/15.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMManagerOrderController.h"
#import "FLMOrderDetailController.h"
#import "FLMClaimOrderController.h"
#import "FLMManageOrderHeaderView.h"
#import "FLMManageOrderListController.h"

@interface FLMManagerOrderController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (nonatomic,strong) FLMManageOrderHeaderView *headerView;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSMutableArray *pagesArr;
@property (strong, nonatomic) NSArray *menuItemArr;
@property (strong, nonatomic) NSArray *picArr;

@end

@implementation FLMManagerOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"订单管理";
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setTitleColor:black forState:UIControlStateNormal];
    [rightButton setTitle:@"认领" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(claimAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = item;

    
    [self addChildViewControllers];
    [self layoutUI];
}

- (NSMutableArray *)pagesArr
{
    if (!_pagesArr) {
        _pagesArr = [[NSMutableArray alloc]init];
    }
    
    return _pagesArr;
}

- (NSArray *)menuItemArr
{
    if (!_menuItemArr) {
        _menuItemArr = [NSArray arrayWithObjects:@"待审核订单",@"淘宝订单",@"商城订单", nil];
    }
    
    return _menuItemArr;
}

- (NSArray *)picArr
{
    if (!_picArr) {
        _picArr = [NSArray arrayWithObjects:@"pre_check_order",@"taobao_order",@"mall_order", nil];
    }
    
    return _picArr;
}

-(void)addChildViewControllers
{
    
    for (NSInteger i=0; i<self.menuItemArr.count; i++) {
        FLMManageOrderListController *subVC = [[FLMManageOrderListController alloc]init];
        subVC.orderTypeName = self.menuItemArr[i];
        [self.pagesArr addObject:subVC];
    }
    
    
}

- (void)layoutUI
{
    if (!_headerView) {
        _headerView = [[FLMManageOrderHeaderView alloc]initWithFrame:CGRectMake(0, 64, ScreenW, 150)];
        [self.view addSubview:_headerView];
        __weak typeof (self)weakSelf = self;
        [_headerView setDidSelectBlock:^(NSInteger index) {
            [weakSelf.pageViewController setViewControllers:@[weakSelf.pagesArr[index]]direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
        }];
        [_headerView configViewWithDataArr:self.picArr];
    }
    
    if (!_pageViewController) {
        _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        _pageViewController.view.frame = CGRectMake(0, CGRectGetMaxY(_headerView.frame), ScreenW,ScreenH-CGRectGetMaxY(_headerView.frame)-64);
//        [_pageViewController setDataSource:self];
        [_pageViewController setDelegate:self];
        [_pageViewController.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
        [self addChildViewController:_pageViewController];
        [self.view addSubview:_pageViewController.view];
        
        if (_pagesArr.count) {
            [_pageViewController setViewControllers:@[self.pagesArr[0]]
                                          direction:UIPageViewControllerNavigationDirectionForward
                                           animated:YES
                                         completion:NULL];
        }

        
    }
}

#pragma mark - 私有方法
- (void)pageViewController:(UIPageViewController *)pageViewController  selectedIndex:(NSInteger)index direction:(UIPageViewControllerNavigationDirection)direction
{
    [pageViewController setViewControllers:@[self.pagesArr[index]]
                                 direction:direction
                                  animated:YES
                                completion:NULL];
}

- (void)claimAction
{
    FLMClaimOrderController *cliamVC = [[FLMClaimOrderController alloc]init];
    [self.navigationController pushViewController:cliamVC animated:YES];
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.pagesArr indexOfObject:viewController];
    
    if ((index == NSNotFound) || (index == 0)) {
        return nil;
    }
    
    return self.pagesArr[--index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.pagesArr indexOfObject:viewController];
    
    if ((index == NSNotFound)||(index+1 >= [self.pagesArr count])) {
        return nil;
    }
    
    return self.pagesArr[++index];
}

- (void)pageViewController:(UIPageViewController *)viewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (!completed){
        return;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
